//person-info.js
//获取应用实例
var app = getApp();

Page({
    data: {
        siteInfo: app.siteInfo,
        isLogin: true,
        thumb: '',
        nickname: '',
    },
    onLoad: function () {
        var that = this;
        app.initPage({'navigationBarTitleText': '收入明细'});
        app.util.footer(that);
        app.util.getUserInfo(function (res) {
            var userInfo = res.memberInfo;
            that.setData({
                userInfo: userInfo,
            });
            if (!userInfo) {
                that.setData({
                    'isLogin': false,
                });
            } else {
                //用户相关信息
                app.util.request({
                    url: 'user/personInfo',
                    showLoading: false,
                    success: function (res) {
                        var data = res.data.data;
                        //设置标题
                        that.setData({
                            'data': data,
                        });
                    }
                });
            }
        });

        let userInfo = wx.getStorageSync('userInfo');
        if (!userInfo || !userInfo.memberInfo.nickname) {
            that.setData({
                'isLogin': false,
            });
        } else {
            that.setData({
                'isLogin': true,
            });
        }
    },
    //提现
    withdraw: function (e) {
        var money = e.target.dataset.money;
        if (money < 1) {
            wx.showModal({
                title: '',
                showCancel: false,
                content: "提现最少金额1元",
                success: function (res) {
                }
            })
            return;
        }
        app.util.request({
            url: 'user/withdraw',
            showLoading: false,
            success: function (res) {
                if (res.data.code == 1) {
                    wx.showToast({
                        title: '提现申请成功',
                        icon: 'success',
                    });
                } else {
                    wx.showModal({
                        title: '提现失败',
                        showCancel: false,
                        content: res.data.msg,
                        success: function (res) {
                        }
                    })
                }
            }
        });
    },
    //下拉刷新
    onPullDownRefresh: function () {
        var that = this;
        var page = getCurrentPages().pop();
        page.onLoad();
        wx.stopPullDownRefresh();
    },
    chooseAddress: function () {
        wx.chooseAddress({
            success: function (res) {
                // console.log(res.userName)
                // console.log(res.postalCode)
                // console.log(res.provinceName)
                // console.log(res.cityName)
                // console.log(res.countyName)
                // console.log(res.detailInfo)
                // console.log(res.nationalCode)
                // console.log(res.telNumber)
            }
        })
    },
    //获取用户信息
    updateUserInfo(result) {
        var app = getApp();
        let loginRefer = wx.getStorageSync('loginRefer');
        var that = this;

        //拿到用户数据时，通过app.util.getUserinfo将加密串传递给服务端
        //服务端会解密，并保存用户数据，生成sessionid返回
        app.util.getUserInfo(function (userInfo) {
            //这回userInfo为用户信息
            //更细本地userInfo
            app.util.request({
                url: 'user/synUser',
                data: {'uid': userInfo.memberInfo.uid},
                showLoading: false,
                header: {
                    contentType: "application/json",
                },
                method: 'POST',
                cachetime: 0,
                success: function (res) {
                    wx.showToast({
                        title: '登录成功',
                        icon: 'none',
                    });
                    if (loginRefer) {
                        wx.removeStorageSync('loginRefer');
                        wx.redirectTo({
                            url: loginRefer,
                        });
                    } else {
                        //当前页面刷新
                        var page = getCurrentPages().pop();
                        page.onLoad();
                    }
                }
            });
        }, result.detail)
    },
    /**
     * 底部导航跳转
     */
    footerRedirect: function (e) {
        var type = e.target.dataset.type;
        if (type == 2) {
            var jumpData = e.target.dataset.jumpdata;
            //跳转到小程序
            wx.navigateToMiniProgram({
                appId: jumpData.appid,
                path: jumpData.url,
                extraData: jumpData.extra_data,
                envVersion: jumpData.env_version,
                success(res) {
                    // 打开成功
                },
                fail(res) {

                }
            })
            return;
        } else {
            wx.redirectTo({
                url: e.target.dataset.pagepath,
            });
        }
    },
    /**
     * 底部导航跳转
     */
    footerRedirect: function (e) {
        var type = e.target.dataset.type;

        if (type == 2) {
            var jumpData = e.target.dataset.jumpdata;
            //跳转到小程序
            wx.navigateToMiniProgram({
                appId: jumpData.appid,
                path: jumpData.url,
                extraData: jumpData.extra_data,
                envVersion: jumpData.env_version,
                success(res) {
                    // 打开成功
                },
                fail(res) {

                }
            })
            return;
        } else {
            wx.redirectTo({
                url: e.target.dataset.pagepath,
            });
        }
    }
})  